package com.memory.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.memory.common.BaseContext;
import com.memory.common.R;
import com.memory.daomain.AddressBook;
import com.memory.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 邓哈哈
 * 2023/1/28 18:51
 * Function:
 * Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增收货地址
     *
     * @param addressBook
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody AddressBook addressBook) {
        //设置该新增地址的userId
        addressBook.setUserId(BaseContext.getCurrentId());
        //新增地址
        boolean save = addressBookService.save(addressBook);

        return save ? R.success("新增地址成功!") : R.error("新增地址失败!");
    }

    /**
     * 列出所有收货地址
     *
     * @param
     * @return
     */
    @GetMapping("/list")
    public R<List<AddressBook>> list() {
        //获取该用户id
        Long userId = BaseContext.getCurrentId();
        //根据id获取旗下所有收货地址
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();

        //设置查询条件
        lqw.eq(userId != null, AddressBook::getUserId, userId);
        List<AddressBook> addressBookList = addressBookService.list(lqw);

        return (addressBookList != null) ? R.success(addressBookList) : R.error("未知错误!");
    }

    /**
     * 修改数据前的回显
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<AddressBook> getById(@PathVariable Long id) {
        //根据地址id拿取对应地址
        AddressBook addressBook = addressBookService.getById(id);

        return addressBook != null ? R.success(addressBook) : R.error("回显错误");
    }

    /**
     * 修改收货地址
     *
     * @param addressBook
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook) {
        boolean updateById = addressBookService.updateById(addressBook);

        return updateById ? R.success("收货地址修改成功") : R.error("收货地址修改失败");
    }

    /**
     * 删除收货地址
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long ids) {
        boolean removeById = addressBookService.removeById(ids);

        return removeById ? R.success("收货地址删除成功") : R.error("收货地址删除失败");
    }

    /**
     * 设置默认地址
     *
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public R<String> defaultAddress(@RequestBody AddressBook addressBook) {
        //思路:拿到当前用户的所有收货地址Address,再将所有收货地址Address的状态设为非默认,再将该选中地址的状态设为默认default
        //拿取当前用户id
        Long userId = BaseContext.getCurrentId();

        LambdaUpdateWrapper<AddressBook> luw = new LambdaUpdateWrapper<>();

        //1.拿到当前用户的所有收货地址Address
        luw.eq(userId != null, AddressBook::getUserId, userId);

        //2.所有收货地址Address的状态设为非默认
        luw.set(AddressBook::getIsDefault, 0);
        boolean update = addressBookService.update(luw);//执行修改

        //3.将该选中地址的状态设为默认default
        addressBook.setIsDefault(1);
        boolean updateById = addressBookService.updateById(addressBook);//执行修改
        log.info("??");

        return update && updateById ? R.success("成功设置默认地址") : R.error("未知错误,设置失败");
    }

    /**
     * 支付订单时拿取默认收货地址
     *
     * @return
     */
    @GetMapping("/default")
    public R<AddressBook> getDefaultAddress() {
        //思路:拿到当前用户的所有收货地址Address,再将所有收货地址Address的状态设为非默认,再将该选中地址的状态设为默认default
        //拿取当前用户id
        Long userId = BaseContext.getCurrentId();

        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();

        //1.拿到当前用户的默认地址
        //设置查询条件
        lqw.eq(userId != null, AddressBook::getUserId, userId);//当前用户地址
        lqw.eq(AddressBook::getIsDefault, 1);//默认地址
        //执行查询
        AddressBook addressBook = addressBookService.getOne(lqw);

        return addressBook != null ? R.success(addressBook) : R.error("未知错误");
    }


}
